/*
 *---------------------------------------------------------------------------
 *
 * Copyright (c) 2020, 2021 Analog Devices, Inc. All Rights Reserved.
 * This software is proprietary to Analog Devices, Inc.
 * and its licensors.By using this software you agree to the terms of the
 * associated Analog Devices Software License Agreement.
 *
 *---------------------------------------------------------------------------
 */

#include "main.h"

#define ADIN1110_INIT_ITER  (5)

/* Example configuration */
uint8_t devMem[ADIN1110_DEVICE_SIZE];

adin1110_DriverConfig_t drvConfig = {
    .pDevMem    = (void *)devMem,
    .devMemSize = sizeof(devMem),
    .fcsCheckEn = true,
};

int main(void)
{
    adi_eth_Result_e        result;
    uint32_t                error;
    adin1110_DeviceStruct_t dev;
    adin1110_DeviceHandle_t hDevice = &dev;
    uint32_t                heartbeatCheckTime = 0;

    /****** System Init *****/
    error = BSP_InitSystem();
    DEBUG_RESULT("BSP_InitSystem", error, 0);

    /* Hardware reset of the ADIN1110 using a host GPIO */
    BSP_HWReset(true);

    /****** Driver Init *****/
    for (uint32_t i = 0; i < ADIN1110_INIT_ITER; i++)
    {
        result = adin1110_Init(hDevice, &drvConfig);
        if (result == ADI_ETH_SUCCESS)
        {
            break;
        }
    }
    DEBUG_RESULT("No MACPHY device found", result, ADI_ETH_SUCCESS);

    result = adin1110_AddAddressFilter(hDevice, &macAddr[0][0], NULL, 0);
    DEBUG_RESULT("adin1110_AddAddressFilter", result, ADI_ETH_SUCCESS);

    result = adin1110_AddAddressFilter(hDevice, &macAddr[1][0], NULL, 0);
    DEBUG_RESULT("adin1110_AddAddressFilter", result, ADI_ETH_SUCCESS);

    result = adin1110_SyncConfig(hDevice);
    DEBUG_RESULT("adin1110_SyncConfig", result, ADI_ETH_SUCCESS);

    result = adin1110_SetLoopbackMode(hDevice, ADI_PHY_LOOPBACK_MACIF_REMOTE);
    DEBUG_RESULT("adin1110_SetLoopbackMode", result, ADI_ETH_SUCCESS);

    result = adin1110_Enable(hDevice);
    DEBUG_RESULT("adin1110_Enable", result, ADI_ETH_SUCCESS);

    while (1)
    {
        uint32_t now  = BSP_SysNow();

        /* Heartbeat pulse approximately once every 250ms. */
        if (now - heartbeatCheckTime >= 250)
        {
            heartbeatCheckTime = now;
            BSP_HeartBeat();
        }

    }
}
